import storage from 'store'
import { checkSms, login, logout } from '@/api/login'
import { getInfo } from '@/api/user'
import { ACCESS_TOKEN, GROUP_TYPE, MSG_COUNT, VIP, CURRENT_COMPANY } from '@/store/mutation-types'
import { getMessageByRead } from '@/api/message'

const user = {
  state: {
    token: '',
    name: '',
    avatar: '',
    info: {},
    bg: '',
    groupType: '',
    msgCount: '',
    company: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    SET_BG: (state, bg) => {
      state.bg = bg
    },
    SET_TYPE: (state, groupType) => {
      state.groupType = groupType
    },
    SET_COUNT: (state, msgCount) => {
      state.msgCount = msgCount
    },
    SET_VIP: (state, vip) => {
      state.vip = vip
    },
    SET_COMPANY: (state, company) => {
      state.company = company
    }
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          const result = response.result
          storage.set(ACCESS_TOKEN, result.access_token, (7 * 24 * 3600 * 1000))
          commit('SET_TOKEN', result.access_token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 验证码登陆
    CodeLogin ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        checkSms(userInfo).then(response => {
          const result = response.result
          storage.set(ACCESS_TOKEN, result.access_token, (7 * 24 * 3600 * 1000))
          commit('SET_TOKEN', result.access_token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const stwkd = response.result.sr_is_kd
          const menus = response.result.antd_menus
          const info = response.result.user_info
          const vip = response.result.vip_receive_types
          if (menus.length > 0) {
            commit('SET_INFO', info)
          } else {
            reject(new Error('getInfo: roles must be a non-null array !'))
          }
          storage.set(GROUP_TYPE, info.group_type, (7 * 24 * 3600 * 1000))
          storage.set(CURRENT_COMPANY, response.result.current_company, (7 * 24 * 3600 * 1000))
          storage.set(VIP, vip, (7 * 24 * 3600 * 1000))
          commit('SET_TYPE', info.group_type)
          commit('SET_COMPANY', response.result.current_company)
          commit('SET_BG', stwkd)
          commit('SET_NAME', info.username)
          commit('SET_AVATAR', info.avatar)
          commit('SET_VIP', vip)
          resolve(info)
        }).catch(error => {
          reject(error)
        })
      })
    },

    GetMessage ({ commit }, msgType) {
      return new Promise(resolve => {
        getMessageByRead(msgType).then(res => {
          storage.set(MSG_COUNT, res.result.total_count)
          commit('SET_COUNT', res.result.total_count)
          resolve(res)
        })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      return new Promise((resolve) => {
        logout(state.token).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        }).finally(() => {
          commit('SET_TOKEN', '')
          commit('SET_TYPE', '')
          commit('SET_VIP', '')
          commit('SET_COMPANY', '')
          storage.remove(ACCESS_TOKEN)
          storage.remove(GROUP_TYPE)
          storage.remove(CURRENT_COMPANY)
          storage.remove(VIP)
        })
      })
    }
  }
}

export default user
